package com.appspot.alcappmobile;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
 
public class alcmobile extends Activity
{
	private Button SubmitButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.mainpage);

        ImageButton next = (ImageButton) findViewById(R.id.HALL_OF_SHAME);
        next.setColorFilter(Color.argb(255, 255, 255, 255));
        next.setOnClickListener
        (
        		new View.OnClickListener() 
        		{
        			public 
        			void onClick(View view) 
        			{
        				Intent myIntent = new Intent(alcmobile.this,hallofshame.class);
        				alcmobile.this.startActivity(myIntent);
             
        			}

        		}
        );

        
        
        ImageButton next1 = (ImageButton) findViewById(R.id.ALC_APP_TITLE);
        next1.setOnClickListener
        (
        		new View.OnClickListener() 
        		{
        			public void onClick(View view) 
        			{
        				Intent myIntent = new Intent(alcmobile.this,about.class);
        				alcmobile.this.startActivity(myIntent);
             
        			}

        		}
        );
        
        
        
        this.SubmitButton = (Button)this.findViewById(R.id.Submit);
        this.SubmitButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        	  TextView textView = (TextView)findViewById(R.id.outputBAC);
              RestClient client = new RestClient("http://alcappmobile.appspot.com/listenforrestclient");
              //Rest client retrieves all of the HTML and stores it in 2 array lists, "params" and "headers"          
              //client.AddParam("message", "Nav");
              
              
              
              try
              {
                  client.Execute(RequestMethod.GET);
              }
              catch (Exception e)
              {
                  textView.setText(e.getMessage());
              }
              
              String response = client.getResponse();
              
              //parsing with userName and response
              String r = response;
              int i = -1;
              Integer weightval = -1;
              
              // retrieve user name from EditText box
              EditText nameBox = (EditText) findViewById(R.id.NAME);
              String userName = nameBox.getText().toString().trim();
              i = r.indexOf(userName);
              
              EditText weightBox = (EditText) findViewById(R.id.Weight);
              
              if(weightBox.getText().toString().equals("")) 
              {
            	  weightval = -1;
              }
              else
              {
            	  String weightstring = weightBox.getText().toString();
            	  weightval = Integer.valueOf(weightstring);
              }
     
              
              if(i != -1 && weightval > 0)
              {
            	  
		              r = r.substring(i);
		              i = r.indexOf("</td></tr></table>");
		              r = r.substring(0,i);
		              
		              String w = r;
		              String b = r;
		              String s = r;
		              
		              int temp;
		              int wNum = 0, bNum = 0, sNum = 0;
		              
		              while(b.indexOf("beer") != -1)
		              {
		              	bNum++;
		              	temp = b.indexOf("beer");
		              	b = b.substring(temp+4);
		              }
		              
		             
		              while(w.indexOf("wine") != -1)
		              {
		              	wNum++;
		              	temp = w.indexOf("wine");
		              	w = w.substring(temp+4);
		              }
		              
		              
		              while(s.indexOf("shot") != -1)
		              {
		 
		            	sNum++;
		              	temp = s.indexOf("shot");
		              	s = s.substring(temp+4);
		              }
	             
		              double currentBAC = BAC(wNum,sNum,bNum,weightval,4);
		              //reduce BAC to 2 decimal places             
		              String myString = String.format("%.3g", currentBAC);
		              myString += "%";
		              textView.setText(myString);
            	
        	}    
          }
        });
    }
    

    public double roundTwoDecimals(double d) 
    {
    	DecimalFormat twoDForm = new DecimalFormat("#.#");
    	return Double.valueOf(twoDForm.format(d));
    
    }
    
    public double BAC(int winenum, int shotnum, int beernum, int weight, int hours)
    {   
    // %BAC = (A x 5.14/W x g) - .015 x H 
    // A = Actual # of ozs of Alcohol in Drink 
    // W = Weight, g = .73 for men, r = .66 for women 
    // H = Height 
    int beeramt = 12;
    int wineamt = 5;
    
    // calculate the total ounces of pure alcohol
    double alcohol = (beeramt*beernum*.07) + (winenum*wineamt*0.12) + (shotnum*0.4);
    // float BAC=0, A=0, W=0, g=0, h=0;
    // There should be code here to get information from user to input into variables 
    
    
    // End Input from user 
    
    return (((alcohol*5.14) / (weight*0.73)) - (0.015*hours));

    }
}